﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STKMIS.Model;
using STKMIS.DAL;
using System.Data;

namespace STKMIS.BLL
{
    public class Purchasecs
    {
        public Purchasecs() { }

        /// <summary>
        /// 采购入库
        /// </summary>
        /// <param name="purchaseInfo"></param>
        /// <param name="purchaseItemList"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool InsertPurchaseAndItem(PurchaseModel purchaseInfo, List<PurchaseItemModel> purchaseItemList, ref string msg)
        {
            bool isTrue = true;
            try
            {
                using (DBTransaction tran = new DBTransaction(SqlDBServer.SqlConnStringModelDb))
                {
                    tran.BeginTransaction();
                    isTrue = InsertPurchase(purchaseInfo, tran, ref msg);
                    if (isTrue)
                    {
                        foreach (PurchaseItemModel purchaseItemInfo in purchaseItemList)
                        {
                            purchaseItemInfo.PurchaseID = purchaseInfo.PurchaseID;
                            isTrue = PurchaseItem.InsertPurchaseItem(purchaseItemInfo, tran, ref msg);
                            if (!isTrue)
                            {
                                isTrue = false;
                                break;
                            }
                            else
                            {
                                foreach (ProductsAndUnitModel productsAndUnitInfo in purchaseItemInfo.ProductsAndUnitList)
                                {
                                    productsAndUnitInfo.PurchaseItemID = purchaseItemInfo.PurchaseItemID;
                                    isTrue = ProductsAndUnit.InsertProductsAndUnit(productsAndUnitInfo, tran, ref msg);
                                    if (!isTrue)
                                    {
                                        isTrue = false;
                                        break;
                                    }
                                }

                            }
                            if (!isTrue)
                            { break; }
                        }
                        if (isTrue)
                        {
                            tran.Commit();
                        }
                        else
                        {
                            isTrue = false;
                            tran.Rollback();
                        }
                    }
                    else
                    {
                        isTrue = false;
                        tran.Rollback();
                    }

                }
                return isTrue;
            }
            catch
            {
                msg = "采购入库失败！";
                isTrue = false;
            }
            return isTrue;
        }
        /// <summary>
        /// 采购入库主表
        /// </summary>
        /// <param name="purchaseInfo"></param>
        /// <param name="tran"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool InsertPurchase(PurchaseModel purchaseInfo, DBTransaction tran, ref string msg)
        {
            int executedRows = 0;
            bool isTrue = true;
            try
            {

                List<SqlDBParameter> param = new List<SqlDBParameter>();

                param.Add(new SqlDBParameter("@ProductSupplierID", DbType.Int32, purchaseInfo.ProductSupplierID));
                param.Add(new SqlDBParameter("@PurchaseSumMoney", DbType.Decimal, purchaseInfo.PurchaseSumMoney));
                param.Add(new SqlDBParameter("@PurchaseRemark", DbType.String, purchaseInfo.PurchaseRemark));
                param.Add(new SqlDBParameter("@PurchaseUserID", DbType.Int32, purchaseInfo.PurchaseUserID));
                param.Add(new SqlDBParameter("@BillState", DbType.String, purchaseInfo.BillState));

                param.Add(new SqlDBParameter("@PurchaseID", DbType.Int32, ParameterDirection.Output));
                param.Add(new SqlDBParameter("@PurchaseBillNo", DbType.String, ParameterDirection.Output, 50));
                param.Add(new SqlDBParameter("@Msg", DbType.String, ParameterDirection.Output, 50));


                executedRows = SqlDBServer.ExecuteStoredProc("proc_PurchaseInsert", param, tran.Transaction, SqlDBServer.SqlConnStringModelDb);

                if (executedRows <= 0)
                {
                    isTrue = false;
                }
                else
                {
                    if (param[5].Value != null && param[6].Value != null && param[7].Value != null)
                    {
                        purchaseInfo.PurchaseBillNo = param[6].Value.ToString();
                        purchaseInfo.PurchaseID = Convert.ToInt32(param[5].Value.ToString());
                        msg = param[7].Value.ToString();
                    }
                    else
                    {
                        isTrue = false;
                    }
                }

                return isTrue;
            }
            catch
            {
                return false;
            }
        }
    }
}
